import axios from 'axios'
import qs from 'qs'
//封装axios,统一处理1001返回码
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000,
  })
service.interceptors.request.use(config => {
  config.method == 'post' ? config.data = qs.stringify({
    ...config.data
  }) : config.data = ({
    ...config.params
  });
  config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  if (localStorage.getItem('accessToken')) {
    config.headers['authorization'] = localStorage.getItem('accessToken');
  }
  return config;
}, error => {
  Promise.reject(error);
});
service.interceptors.response.use(response => {
  if (response.data.code == '1001' || response.data.code == '1003') {
    window.location.href = '#/login';
    localStorage.clear();
  }
  return response.data;
}, error => {
  // window.location.href='#/login';
  console.log('error');
  console.log(error);
  console.log(JSON.stringify(error));
  return Promise.reject(error);
});

export default service;